package com.example.a26436.newpractise.practise2;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.a26436.newpractise.R;

import java.util.List;

/**
 * Created by 26436 on 2020/9/20.
 */

public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ArticleHolder> {
    private List<ArticleEnity.DataBean.DatasBean> mList;
    private Context mContext;
    private View myItemView;

    public ArticleAdapter(View myItemView) {
        this.myItemView = myItemView;
    }

    //创建构造方法
    public ArticleAdapter(List<ArticleEnity.DataBean.DatasBean> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }



    //创建viewholder
    @Override
    public ArticleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_artcle_artcle_adapter, parent, false);
        return new ArticleHolder(inflate);
    }
    //设置数据
    @Override
    public void onBindViewHolder(final ArticleHolder holder, int position) {
        final ArticleEnity.DataBean.DatasBean datasBean = mList.get(position);
        String superChapterName = datasBean.getSuperChapterName();
        String title = datasBean.getTitle();
        holder.tvChaptername.setText(superChapterName);
        holder.tvTitle.setText(title);
        if (mlistener != null) {
            holder.myItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mlistener.onArticleItemClick(holder.getAdapterPosition(),datasBean);
                }
            });
        }


    }
    //获取条目数量
    @Override
    public int getItemCount() {
        return mList.size();
    }


    public static class ArticleHolder extends RecyclerView.ViewHolder{

        private  TextView tvTitle;
        private  TextView tvChaptername;
        private View myItemView;

        public ArticleHolder(View itemView) {
            super(itemView);
            this.myItemView = itemView;
            tvChaptername = itemView.findViewById(R.id.tv_chapter_name_item);
            tvTitle = itemView.findViewById(R.id.tv_title_item);
        }

    }
    private onArticleItemClickListener mlistener;
    public void setOnArticleItemClickListener(onArticleItemClickListener listener){
        this.mlistener = listener;
    }

    public interface onArticleItemClickListener{
        void onArticleItemClick(int position,
                                ArticleEnity.DataBean.DatasBean datasBean);

    }
}
